package com.voice.server.controller;

import com.voice.server.api.*;
import com.voice.server.command.impl.AudioFileCommand;
import com.voice.server.command.impl.AudioTtlCommand;
import com.voice.server.command.impl.AudioUrlCommand;
import com.voice.server.entity.AppToken;
import com.voice.server.enums.ResultEnum;
import com.voice.server.exception.SystemException;
import com.voice.server.service.AppTokenService;
import com.wangjiacai.utils.output.ResultVO;
import com.wangjiacai.utils.utils.ResultVOUtil;
import com.wangjiacai.utils.utils.TokenUtil;
import com.wangjiacai.utils.utils.ValidatorUtil;
import com.wangjiacai.utils.web.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

@RestController
@RequestMapping(value = "/api")
public class ApiController {

    private long expire=60*60*2;

    @Autowired
    private AppTokenService appTokenService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private AudioTtlCommand audioTtlCommand;
    @Autowired
    private AudioFileCommand audioFileCommand;
    @Autowired
    private AudioUrlCommand audioUrlCommand;

    @GetMapping("/token")
    public ResultVO getApiToken(@RequestHeader("x-ca-key") String appKey){
        AppToken appToken=appTokenService.findByAppKey(appKey);
        String token= TokenUtil.getUUID32();
        AccessToken accessToken=new AccessToken(token,expire);
        redisService.set(token,appToken,expire);
        return ResultVOUtil.success(accessToken);
    }

    @PutMapping(value = "/token")
    public ResultVO putApiToken(@RequestHeader String token){
        AppToken appToken=redisService.get(token,AppToken.class);
        if(appToken==null) throw new SystemException(ResultEnum.TokenExpire);
        redisService.delete(token);
        String refreshToken= TokenUtil.getUUID32();
        AccessToken accessToken=new AccessToken(refreshToken,expire);
        redisService.set(refreshToken,appToken,expire);
        return ResultVOUtil.success(accessToken);
    }

    @PostMapping("/device/tts")
    public ResultVO postTtsText(@RequestHeader String token,@RequestBody @Valid TtsRequest ttsRequest, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        AppToken appToken = redisService.get(token, AppToken.class);
        audioTtlCommand.execute(appToken.getUserId(),ttsRequest.getDeviceKey(),ttsRequest.getText());
        return ResultVOUtil.success();
    }

    @PostMapping("/device/file")
    public ResultVO postFile(@RequestHeader String token,@Valid FileRequest fileRequest, BindingResult bindingResult) throws IOException {
        ValidatorUtil.valid(bindingResult);
        AppToken appToken = redisService.get(token, AppToken.class);
        audioFileCommand.execute(appToken.getUserId(),fileRequest.getDeviceKey(),fileRequest.getFile().getInputStream());
        return ResultVOUtil.success();
    }

    @PostMapping("/device/url")
    public ResultVO postFile(@RequestHeader String token,@RequestBody @Valid UrlRequest urlRequest, BindingResult bindingResult) throws MalformedURLException {
        ValidatorUtil.valid(bindingResult);
        if(! urlRequest.getUrl().matches("(http|https):\\/\\/[^\\s]+\\.[^\\s]+")) throw new SystemException(ResultEnum.URLFormatError.getCode(),"url格式不正确");
        URL url = new URL(urlRequest.getUrl());
        AppToken appToken = redisService.get(token, AppToken.class);
        audioUrlCommand.execute(appToken.getUserId(),urlRequest.getDeviceKey(),url);
        return ResultVOUtil.success();
    }

}
